package com.lwj.liwanjia.view.activity.address;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lwj.liwanjia.R;
import com.lwj.liwanjia.adapter.AddressAdapter;
import com.lwj.liwanjia.base.BaseActivity;
import com.lwj.liwanjia.bean.AddressBean;
import com.lwj.liwanjia.bean.DeleteAddressBean;
import com.lwj.liwanjia.bean.FirstAddressBean;
import com.lwj.liwanjia.impl.AddressView;
import com.lwj.liwanjia.impl.DeleteAddressView;
import com.lwj.liwanjia.impl.FirstAddressView;
import com.lwj.liwanjia.presenter.AddressPresenter;
import com.lwj.liwanjia.presenter.DeleteAddressPresenter;
import com.lwj.liwanjia.presenter.FirstAddressPresenter;
import com.lwj.liwanjia.utils.SpfUtils;
import com.lwj.liwanjia.view.activity.login.LoginActivity;
import com.superrecycleview.superlibrary.adapter.SuperBaseAdapter;
import com.superrecycleview.superlibrary.recycleview.SuperRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 地址管理
 */
public class AddressActivity extends BaseActivity implements AddressView, DeleteAddressView, FirstAddressView {

    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rlv_address)
    SuperRecyclerView rlvAddress;
    @BindView(R.id.rl_add_address)
    RelativeLayout rlAddAddress;
    @BindView(R.id.rl_detail_address)
    RelativeLayout rlDetailAddress;//有地址
    @BindView(R.id.rl_xinzengdizhi)
    RelativeLayout rlXinzenAddress;//无地址
    @BindView(R.id.progress)
    ProgressBar progressBar;

    private List<AddressBean.DataBean> list = new ArrayList<>();
    private AddressAdapter adapter;
    private AddressPresenter addressPresenter;
    private DeleteAddressPresenter deleteAddressPresenter;
    private FirstAddressPresenter firstPresenter;
    private String token;
    private String type;

    @Override
    protected void initView() {
        tvLeft.setVisibility(View.VISIBLE);
        tvTitle.setText("地址管理");
        Intent intent = getIntent();
        type = intent.getStringExtra("type");
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        rlvAddress.setLayoutManager(manager);
        rlvAddress.setRefreshEnabled(false);
        rlvAddress.setLoadMoreEnabled(false);
        adapter = new AddressAdapter(this, list);
        rlvAddress.setAdapter(adapter);
        token = SpfUtils.getSpfUtils(this).getToken();
        addressPresenter = new AddressPresenter(this);
        deleteAddressPresenter = new DeleteAddressPresenter(this);
        firstPresenter = new FirstAddressPresenter(this);
        listener();
    }

    private void listener() {
//        地址回调
        adapter.setOnItemClickListener(new SuperBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, Object item, int position) {
                if (type != null) {
                    Intent intent = new Intent();
                    intent.putExtra("address", list.get(position));
                    setResult(RESULT_OK, intent);
                    finish();
                }
            }
        });
        adapter.setOnItemChildClickListener(new SuperBaseAdapter.OnRecyclerViewItemChildClickListener() {
            @Override
            public void onItemChildClick(SuperBaseAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.tv_delete://删除地址
                        deleteAddressPresenter.deleteAddress(token, list.get(position).getCondigneeId());
                        break;
                    case R.id.tv_edit://修改地址
                        //需要传bean
                        Intent intent = new Intent(AddressActivity.this, BianjiAddressActivity.class);
                        intent.putExtra("consigneeId", list.get(position).getCondigneeId());
                        startActivity(intent);
                        break;
                    case R.id.ll_moren://设为默认地址
                        firstPresenter.firstAddress(token, list.get(position).getCondigneeId());
                        break;
                }
            }
        });
    }
    @Override
    protected void initData() {
        addressPresenter.getAddressData(token);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_address;
    }


    @OnClick({R.id.tv_left, R.id.rl_add_address, R.id.rl_xinzengdizhi})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_left://返回
                finish();
                break;
            case R.id.rl_add_address://添加地址
                Intent intent = new Intent(AddressActivity.this, BianjiAddressActivity.class);
                startActivity(intent);
                break;
            case R.id.rl_xinzengdizhi://新增地址
                Intent intent1 = new Intent(AddressActivity.this, BianjiAddressActivity.class);
                startActivity(intent1);
                break;
        }
    }

    @Override
    public void showProgress() {
        progressBar.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideProgress() {
        progressBar.setVisibility(View.GONE);
    }

    @Override
    public void showError(Exception e) {
        Toast.makeText(this, "数据加载失败", Toast.LENGTH_SHORT).show();
    }

    //删除地址
    @Override
    public void deleteAddress(DeleteAddressBean deleteAddressBean) {
        if (deleteAddressBean.getState() == 0) {
            Toast.makeText(this, "访问失败", Toast.LENGTH_SHORT).show();
        } else if (deleteAddressBean.getState() == 1) {
            if (deleteAddressBean.isData()) {
                initData();
            } else {
                Toast.makeText(this, "删除失败", Toast.LENGTH_SHORT).show();
            }
        }
    }

    //    获取所有的地址
    @Override
    public void getAddressData(AddressBean addressBean) {
        if (addressBean.getState() == 0) {
            Toast.makeText(this, "访问失败", Toast.LENGTH_SHORT).show();
        } else if (addressBean.getState() == 1) {
            if (addressBean.getData().size() > 0) {
                rlXinzenAddress.setVisibility(View.GONE);
                rlDetailAddress.setVisibility(View.VISIBLE);
                list.clear();
                list.addAll(addressBean.getData());
                adapter.notifyDataSetChanged();
            } else {
                rlXinzenAddress.setVisibility(View.VISIBLE);
                rlDetailAddress.setVisibility(View.GONE);
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (addressPresenter != null) {
            addressPresenter = null;
        }
        if (deleteAddressPresenter != null) {
            deleteAddressPresenter = null;
        }
        if (firstPresenter != null) {
            firstPresenter = null;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    //    设置默认地址
    @Override
    public void firstAddress(FirstAddressBean firstAddressBean) {
        if (firstAddressBean.getState() == 0) {
            Toast.makeText(this, "访问失败", Toast.LENGTH_SHORT).show();
        } else if (firstAddressBean.getState() == 1) {
            if (firstAddressBean.isData()) {
                initData();
            } else {
                Toast.makeText(this, "设为默认地址失败", Toast.LENGTH_SHORT).show();
            }
        }
    }
}
